//package com.wjk.kylin.mall.gateway.filter;
//
//import com.wjk.kylin.mall.common.core.constant.Constants;
//import com.wjk.kylin.mall.common.core.exception.BaseException;
//import com.wjk.kylin.mall.common.redis.utils.RedisUtil;
//import com.wjk.kylin.mall.gateway.config.CaptchaConfig;
//import com.wjk.kylin.mall.gateway.config.IgnoreUrlsConfig;
//import com.wjk.kylin.mall.gateway.exception.CaptchaException;
//import com.wjk.kylin.mall.gateway.util.WebUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.gateway.filter.GatewayFilter;
//import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.stereotype.Component;
//import org.springframework.util.MultiValueMap;
//
///**
// * 验证码过滤器
// *
// * @author ruoyi
// */
//@Slf4j
//@Component
//public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object> {
//    @Autowired
//    private RedisUtil redisUtil;
//    @Autowired
//    private CaptchaConfig captchaConfig;
//
//    private final static String[] VALIDATE_URL = new String[]{"/auth/oauth/token"};
//    private static final String CODE = "code";
//    private static final String UUID = "uuid";
//
//
//    @Override
//    public GatewayFilter apply(Object config) {
//        return (exchange, chain) -> {
//            ServerHttpRequest request = exchange.getRequest();
//            String path = request.getURI().getPath();
//            log.info("验证码过滤-访问路径:[{}],是否开启验证码:[{}]", path, captchaConfig.getEnabled());
//            // 非登录/注册请求或验证码关闭，不处理
//            if (!StringUtils.containsAnyIgnoreCase(path, VALIDATE_URL) || !captchaConfig.getEnabled()) {
//                return chain.filter(exchange);
//            }
//
//            //验证是否为后台登录
//            String clientId = request.getQueryParams().getFirst(Constants.CLIENT_ID_KEY);
//            log.info("验证码过滤-客户端ID:[{}]", clientId);
//            if (!Constants.ADMIN_CLIENT_ID.equals(clientId)) {
//                return chain.filter(exchange);
//            }
//
//            try {
//                checkCaptcha(request);
//            } catch (Exception e) {
//                return WebUtils.webFluxResponseWriter(exchange.getResponse(), e.getMessage());
//            }
//            return chain.filter(exchange);
//        };
//    }
//
//    /**
//     * 校验验证码
//     */
//    public void checkCaptcha(ServerHttpRequest serverHttpRequest) {
//        MultiValueMap<String, String> queryParams = serverHttpRequest.getQueryParams();
//        String uuid = queryParams.getFirst(UUID);
//        String code = queryParams.getFirst(CODE);
//
//        String verifyKey = Constants.ADMIN_CAPTCHA_CODE_KEY + uuid;
//        String captcha = (String) redisUtil.get(verifyKey);
//
//        //删除验证码 redis
//        redisUtil.del(verifyKey);
//
//        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(uuid)) {
//            throw new CaptchaException("验证码不能为空");
//        } else if (StringUtils.isBlank(captcha)) {
//            throw new CaptchaException("验证码失效，请重新获取");
//        } else if (!StringUtils.equalsIgnoreCase(captcha, code)) {
//            throw new CaptchaException("验证码错误，请重新输入");
//        }
//    }
////
////    private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest) {
////        // 获取请求体
////        Flux<DataBuffer> body = serverHttpRequest.getBody();
////        AtomicReference<String> bodyRef = new AtomicReference<>();
////        body.subscribe(buffer -> {
////            CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
////            DataBufferUtils.release(buffer);
////            bodyRef.set(charBuffer.toString());
////        });
////        return bodyRef.get();
////    }
//}
